<template>
    <view class="order-state-menu" v-if="configObj">
        <text class="btn btn-gray" v-if="[0, 1, 12].indexOf(item.State) > -1" @click="click('Cancel')">取消订单</text>
        <!-- <text class="btn btn-success" v-if="[0,12].indexOf(item.State)>-1" @click="click('Pay')">立即支付</text></text> -->
        <text
            class="btn btn-gray"
            v-if="[0, 27].indexOf(item.State) > -1 && item.RejectPayMsg && false"
            @click="click('RejctMsg')"
            >驳回理由</text
        >
        <text
            class="btn btn-success"
            v-if="[6].indexOf(item.State) > -1 && item.OldOrderId == 0 && (configObj[14] == 0 || item.Type <= 2)"
            @click="click('Take')"
            >确认收货</text
        >
        <text
            class="btn btn-success"
            v-if="[6].indexOf(item.State) > -1 && item.OldOrderId == 0 && configObj[14] == 1 && item.Type >= 4"
            @click="click('TakeWithSecurityCode')"
            >确认收货</text
        >
        <text
            class="btn btn-gray"
            v-if="
                [6, 19].indexOf(item.State) > -1 &&
                configObj[30] == 1 &&
                [11, 14].indexOf(item.Type) == -1 &&
                item.OldOrderId == 0
            "
            @click="click('ShowReturn')"
            >申请退货</text
        >
        <text
            class="btn btn-gray"
            v-if="
                [2, 13, 27].indexOf(item.State) > -1 &&
                configObj[30] == 1 &&
                [11, 14].indexOf(item.Type) == -1 &&
                item.IsCashPay == 0 &&
                item.ArrearsPrice <= 0 &&
                false
            "
            @click="click('ShowRefund')"
            >申请退款</text
        >
        <text
            class="btn btn-gray"
            v-if="
                [8].indexOf(item.State) > -1 && configObj[30] == 1 && item.Type != 16 && item.OldOrderId == 0 && false
            "
            @click="click('ShowReturnExpress')"
            >填写退货信息</text
        >
        <text
            class="btn btn-gray"
            v-if="islist && item.ExpressNumber && false"
            @click="click('ShowOutLogisticsInfoList')"
            >查看物流</text
        >
        <text class="btn btn-gray" v-if="[10].indexOf(item.State) > -1 && !item.HasComment" @click="click('Comment')"
            >分享评价</text
        >
        <text
            class="btn btn-success-hollow"
            v-if="[0, 12].indexOf(item.State) == -1 && [0, 2, 4].indexOf(item.Type) > -1"
            @click="click('BuyAgain')"
            >再次购买</text
        >
        <text
            class="btn btn-gray"
            v-if="item.GroupOrderId > 0 && item.GroupState == 1 && item.LastTime != null && false"
            @click="click('InviteUsr')"
            >立即邀请</text
        >
        <view
            class="btn btn-gray"
            v-if="[0, 12].indexOf(item.State) == -1 && item.GroupOrderId > 0 && item.GroupState != 1 && false"
            @click="click('InviteUsr')">
            <text v-if="item.Type != 14">拼团详情</text>
            <text v-if="item.Type == 14">邀请详情</text>
        </view>
        <text
            class="btn btn-gray"
            v-if="
                [2, 13].indexOf(item.State) > -1 && (item.Type == 10 || item.PostType == 1) && item.Type >= 4 && false
            "
            @click="click('GetOrderIdCode')"
            >提货码</text
        >
        <text
            class="btn btn-success"
            v-if="item.ArrearsPrice > 0 && item.State != 11"
            @click="goUrl('/pages_supplier/packet/order/waitPayList?orderId=' + item.OrderId)"
            >立即支付</text
        >

        <uni-popup ref="popTip" type="dialog">
            <uni-popup-dialog
                type="text"
                title="温馨提示"
                :content="popTipData.title"
                :before-close="true"
                @close="cancelIt"
                @confirm="doIt"></uni-popup-dialog>
        </uni-popup>
        <uni-popup ref="popReturnTip" type="center" :animation="false">
            <view class="pop-return-box">
                <view class="pop-return-title">
                    <text>{{ popTipData.title }}</text>
                </view>
                <view class="return-msg">
                    <card-cell-input
                        :showLabel="false"
                        ref="returnMsg"
                        :inputType="'text'"
                        placeholder="请输入退款原因"></card-cell-input>
                </view>
                <text class="img-title">上传凭证</text>
                <upload-img :imageList="imageList" @chooseImage="chooseImage" @del="del"></upload-img>
                <view class="action">
                    <button class="btn btn-gray" @click="cancelReturn">取消</button>
                    <button class="btn btn-success" @click="doReturn">提交</button>
                </view>
            </view>
        </uni-popup>
    </view>
</template>

<script>
import { mapMutations } from "vuex"
import uploadImg from "@/uni-sub-base/components/common/upload-img.vue"
import storage from "@/uni-sub-base/utils/storage.js"
import { orderCancel, payOrder, takeOrder, OrderReturnApply, OrderRefundApply } from "@/service/api/order.js"
import { BuyAgain } from "@/service/api/member.js"
import cardCellInput from "@/components/common/cell/card-cell-input.vue"
export default {
    components: {
        cardCellInput,
        uploadImg,
    },
    props: {
        item: {
            type: Object,
            default: {},
        },
        islist: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            configObj: null,
            popTipData: {
                title: "确定要取消当前订单?",
                type: 0,
            },
            imageList: [],
        }
    },
    mounted() {
        //获取商家配置
        let businessFunctionSettingConfig = storage.getCache("businessFunctionSetting")
        if (businessFunctionSettingConfig && businessFunctionSettingConfig != "") {
            this.configObj = businessFunctionSettingConfig
        } else {
            this.configObj = { 14: 0, 30: 0 }
        }
    },
    methods: {
        ...mapMutations(["UP_CART_VIEW", "UP_ORDER_VIEW"]),
        goUrl(url) {
            this.easyGoTo(url)
        },
        //点击操作
        click(type) {
            let item = this.item
            switch (type) {
                case "Pay":
                    //立即支付
                    this.easyGoTo("/pages_supplier/packet/order/waitPayList?orderId=" + item.OrderId)
                    break
                case "Cancel":
                    //取消订单
                    //二次确认
                    this.popTipData = {
                        type: 0,
                        title: "确定要取消当前订单?",
                    }
                    this.showTip()
                    break
                case "RejctMsg":
                    //驳回理由
                    this.easyToast("驳回理由")
                    break
                case "Take":
                case "TakeWithSecurityCode":
                    //确认收货
                    this.popTipData = {
                        type: 1,
                        title: "确认收货?",
                    }
                    this.showTip()
                    break
                case "ShowReturn":
                    //申请退货
                    this.easyGoTo("/pages_supplier/packet/return/index")
                    break
                case "ShowRefund":
                    //申请退款
                    this.popTipData = {
                        type: 4,
                        title: "请填写退款原因",
                    }
                    this.$refs.popReturnTip.open()
                    break
                case "ShowReturnExpress":
                    //填写退货信息
                    this.easyToast("填写退货信息")
                    break
                case "ShowOutLogisticsInfoList":
                    //查看物流
                    this.easyToast("查看物流")
                    break
                case "Comment":
                    //分享评价
                    this.easyGoTo(
                        "/pages_supplier/packet/comment/add?data=" + encodeURIComponent(JSON.stringify(this.item)),
                    )
                    break
                case "BuyAgain":
                    //再次购买
                    this.buyAgain()
                    break
                case "InviteUsr":
                    //立即邀请
                    this.easyToast("立即邀请")
                    break
                case "GetOrderIdCode":
                    //提货码
                    this.easyToast("提货码")
                    break
                default:
                    //这里是没有找到对应的值处理
                    break
            }
        },
        doIt() {
            let type = this.popTipData.type
            switch (type) {
                case 0: {
                    //取消订单
                    let param = {
                        orderId: this.item.OrderId,
                    }
                    orderCancel(param).then((res) => {
                        if (res.State == 1) {
                            this.upOrderView()
                        }
                    })
                    break
                }
                case 1: {
                    //确认收货
                    let param = {
                        orderId: this.item.OrderId,
                    }
                    takeOrder(param).then((res) => {
                        if (res.State == 1) {
                            this.upOrderView()
                        }
                    })
                    break
                }
                default:
                    break
            }
            this.cancelIt()
        },
        //显示确认弹窗
        showTip() {
            this.$refs.popTip.open()
        },
        //关闭弹窗
        cancelIt() {
            this.$refs.popTip.close()
        },
        //更新订单列表
        upOrderView() {
            this.UP_ORDER_VIEW(new Date().getTime())
        },
        //再次购买
        buyAgain() {
            let param = {
                orderId: this.item.OrderId,
            }
            BuyAgain(param).then((res) => {
                if (res.State == 0 && (res.Msg == "已为您跳过库存不足的商品" || res.Msg=="已为您跳过库存不足或不再销售的商品")) {
                    this.easyToast(res.Msg)
                    setTimeout((e) => {
                        this.easyGoTo("/pages/supplier/cart/index")
                    }, 1500)
                } else if (res.State == 1) {
                    this.easyGoTo("/pages/supplier/cart/index")
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        //申请退款
        doReturn() {
            let type = this.popTipData.type
            let returnMsg = this.$refs.returnMsg.getValue()
            //退款
            if (returnMsg.trim() == "") {
                this.easyToast(type == 3 ? "请填写退货原因" : "请填写退款原因")
                return
            }
            if (type == 4) {
                //退款
                let param = {
                    id: this.item.OrderId,
                    reason: returnMsg,
                    image: this.imageList.join(","),
                }
                OrderRefundApply(param).then((res) => {
                    if (res.State) {
                        this.easyToast("已提交申请，请等待审核")
                        this.upOrderView()
                    } else {
                        this.easyToast(res.Msg)
                    }
                    this.cancelReturn()
                })
            } else if (type == 3) {
            }
        },
        //取消退款
        cancelReturn() {
            this.$refs.popReturnTip.close()
        },
        //选择图片
        chooseImage(imglist) {
            this.imageList = imglist
        },
        //删除图片
        del(imglist) {
            this.imageList = imglist
        },
    },
}
</script>

<style lang="scss">
.order-state-menu {
    display: flex;
    flex-direction: row;
    justify-content: flex-end;

    .btn {
        margin-left: $app-page-padding;
    }
}
.pop-return-box {
    background: $color-white;
    border-radius: $border-radius-10;
    padding: $app-page-padding;
    width: 500rpx;
    .pop-return-title {
        display: flex;
        justify-content: center;
        text {
            font-weight: 700;
        }
    }
    .return-msg {
        margin-bottom: 20rpx;
        .card-group-cell {
            margin-left: 0 !important;
        }
    }
    .img-title {
        display: flex;
        color: $app-color-text-tip;
        margin-bottom: 20rpx;
    }
    .action {
        display: flex;
        flex-direction: row;
        .btn {
            flex: 1;
            border-radius: $border-radius-10;
            height: 70rpx;
            &:first-child {
                margin-left: 0;
            }
        }
    }
}
</style>
